package component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import pojo.destroy.DestroyBeanByBeanDefinitionRegistryPostProcessor;

/**
 * @author: ZhouBert
 * @date: 2020/8/28
 * @description:	自定义 BeanDefinitionRegistryPostProcessor
 *
 */
public class TestBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {


	/**
	 * 可以看出来： BeanDefinitionRegistryPostProcessor 接口优先调用，允许修改 registry
	 * 这里通过修改 registry,将新的 b.d. 注册到工厂
	 * @param registry the bean definition registry used by the application context
	 * @throws BeansException
	 */
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry");
		registry.registerBeanDefinition("mytest",new AnnotatedGenericBeanDefinition(TestBeanDefinitionRegistryPostProcessorPriorityOrdered.class));

		//2020年9月21日 添加一个 bean 销毁的测试
		AnnotatedGenericBeanDefinition genericBeanDefinition = new AnnotatedGenericBeanDefinition(DestroyBeanByBeanDefinitionRegistryPostProcessor.class);
		genericBeanDefinition.setDestroyMethodName("test");
		registry.registerBeanDefinition("destroyBeanByBeanDefinitionRegistryPostProcessor",genericBeanDefinition);
	}

	/**
	 * BeanFactoryPostProcessor 在和 上面的同个方法中进行调用，不过在之后被调用
	 * @param beanFactory the bean factory used by the application context
	 * @throws BeansException
	 */
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory");
	}
}
